package com.learning.thread.juc.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * ClassName: ReadWriteLockTest
 * Description: 读写锁：多个读锁不互斥，读锁与写锁互斥，写锁与写锁互斥
 * Date: 2016/11/26 17:45
 *
 * @author SAM SHO
 * @version V1.0
 */
public class ReadWriteLockTest {


    public static void main(String[] args) {

        Queue tQueue = new Queue();
        new Thread(new ReadThread(tQueue), "read_1").start();
        new Thread(new ReadThread(tQueue), "read_2").start();

        new Thread(new WriteThread(tQueue), "write_1").start();
        new Thread(new WriteThread(tQueue), "write_2").start();
    }


    /**
     * 读的线程
     */
    static class ReadThread implements Runnable {

        private Queue tQueue;

        public ReadThread(Queue tQueue) {
            this.tQueue = tQueue;
        }

        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                tQueue.read();
            }

        }
    }

    /**
     * 写的线程
     */
    static class WriteThread implements Runnable {

        private Queue tQueue;

        public WriteThread(Queue tQueue) {
            this.tQueue = tQueue;
        }

        @Override
        public void run() {
            for (int i = 3; i < 7; i++) {
                tQueue.write("data_ " + i);
            }

        }
    }

    /**
     * @author SAM-SHO
     * @title 具体业务类
     * @description
     * @Date 2014-8-23
     */
    static class Queue {
        /**
         * 共享数据，只能有一个线程能写该数据，但可以多个线程同时读数据
         */
        private Object data = null;

        /**
         * 使用读写锁
         */
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        /**
         * 读数据的方法
         */
        public void read() {

            readWriteLock.readLock().lock();//上锁
            try {

                System.out.println("开始读数据....." + Thread.currentThread().getName());
                Thread.sleep((long) (Math.random() * 1000));
                System.out.println(Thread.currentThread().getName() + "已经读到数据： " + data);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                readWriteLock.readLock().unlock();//解锁
            }

        }

        /**
         * 写数据的方法
         *
         * @param tData
         */
        public void write(Object tData) {

            readWriteLock.writeLock().lock();//上锁

            try {
                System.out.println("准备写数据....." + Thread.currentThread().getName());
                Thread.sleep((long) (Math.random() * 1000));
                this.data = tData;
                System.out.println(Thread.currentThread().getName() + "已经写完数据： " + data);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                readWriteLock.writeLock().unlock();//解锁
            }

        }

    }

}
